package com.wuyan.locationdog.view

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import com.arcsoft.commonbase.bean.MsgBeanContent
import com.arcsoft.commonbase.bean.TeamMember
import com.arcsoft.commonbase.ext.getLifecycleOwner
import com.arcsoft.commonbase.ext.gone
import com.arcsoft.commonbase.ext.loadImageCircleCar
import com.arcsoft.commonbase.ext.radius
import com.arcsoft.commonbase.ext.visible
import com.arcsoft.commonbase.net.RetrofitManager
import com.wuyan.locationdog.R
import com.wuyan.locationdog.viewmodel.DeviceViewModel
import kotlinx.coroutines.launch
import leifu.shapelibrary.ShapeView
import me.jessyan.autosize.utils.AutoSizeUtils
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.TranslationConfig


class ShareAcceptPopup(
    context: Context?,
    var mMsgBeanContent: MsgBeanContent,
    var sureCallback: ((String) -> Unit)? = null
) :
    BasePopupWindow(context) {



    init {
        contentView = createPopupById(R.layout.pop_share_accapt)
        popupGravity = Gravity.BOTTOM
    }

    override fun onViewCreated(contentView: View) {
        var substring: String = ""
        var subTitle: String = ""

        super.onViewCreated(contentView)
        if (mMsgBeanContent.noticeContent.contains("[")) {
            substring =
                mMsgBeanContent.noticeContent.substring(
                    mMsgBeanContent.noticeContent.indexOf("[") + 1,
                    mMsgBeanContent.noticeContent.indexOf("]")
                )
            subTitle = mMsgBeanContent.noticeContent.substring(
                mMsgBeanContent.noticeContent.indexOf("]") + 1,
                mMsgBeanContent.noticeContent.length
            )

        }

        findViewById<TextView>(R.id.name).text = substring
        findViewById<TextView>(R.id.subTitle).text = subTitle

        findViewById<TextView>(R.id.deviceName).text =
            if (mMsgBeanContent.dogName.isNullOrEmpty()) mMsgBeanContent.deviceName else {
                mMsgBeanContent.dogName
            }
        findViewById<ImageView>(R.id.deviceAvatarUrl).loadImageCircleCar(mMsgBeanContent.dogImg)

        findViewById<ShapeView>(R.id.btnSure).setOnClickListener {
            dismiss()
            sureCallback?.invoke("")
        }
        findViewById<ShapeView>(R.id.btnCancel).setOnClickListener {
            dismiss()
        }

        loadNetData(mMsgBeanContent)
    }

    fun loadNetData(mMsgBeanContent: MsgBeanContent) {
        if (mMsgBeanContent.billType == 3) {
            if (mMsgBeanContent.noticeType == 7) {
                findViewById<ImageView>(R.id.deviceAvatarUrl).gone()
                val teamGridView = findViewById<TeamGridView>(R.id.teamGridView)
                teamGridView.visible()
//                teamGridView.radius(AutoSizeUtils.dp2px(context,27F))
                teamGridView.radius(27F)

                val mViewModel = DeviceViewModel()
                val map = HashMap<String, Any>()
                map["teamInvitationId"] = mMsgBeanContent.billId
                mViewModel.getTeamShareMsgDetail(map)

                mViewModel.mTeamDetail.observe(context.getLifecycleOwner()!!) {
                    it?.content?.teamMembers?.let { teamGridView.setTeamNum(it) }
                }
            }
        }

    }

    override fun onCreateShowAnimation(): Animation? {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.FROM_BOTTOM)
            .toShow()
    }

}
